package com.pengbei.enshrine.common.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pengbei.enshrine.R;
import com.pengbei.enshrine.common.dialog.callback.CommonCallBack;
import com.pengbei.enshrine.common.utils.CommonUtils;

public class CommonDialog {
    private Activity context;
    private Dialog dialog;
    private RelativeLayout rl_rdp;
    private TextView tv_rdp_title;
    private TextView tv_rdp_content;
    private TextView tv_rdp_cancal;
    private TextView tv_rdp_sure;

    private Display display;
    public CommonDialog(Activity context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }
    public CommonDialog builder() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_common, null);

        rl_rdp = (RelativeLayout) view.findViewById(R.id.rl_rdp_common);
        tv_rdp_title = (TextView) view.findViewById(R.id.tv_rdp_title);
        tv_rdp_content = (TextView) view.findViewById(R.id.tv_rdp_content);
        tv_rdp_cancal = (TextView) view.findViewById(R.id.tv_rdp_cancal);
        tv_rdp_sure = (TextView) view.findViewById(R.id.tv_rdp_sure);

        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);

        rl_rdp.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth()),  LinearLayout.LayoutParams.WRAP_CONTENT));

        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); //设置宽度
        lp.height =  LinearLayout.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setAttributes(lp);

        CommonUtils.starAnimation(dialog.getWindow().getDecorView());//增加弹性动画

        return this;
    }

    public CommonDialog show(String title,String content,String sure,String cancal,final CommonCallBack commonCallBack) {

        tv_rdp_title.setText(title+"");
        tv_rdp_content.setText(content+"");
        tv_rdp_cancal.setText(cancal+"");
        tv_rdp_sure.setText(sure+"");

        tv_rdp_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                commonCallBack.Sure();
            }
        });

        tv_rdp_cancal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                commonCallBack.Cancal();
            }
        });

        return this;
    }

    public void show() {
        dialog.show();
    }




}